import { data } from "react-router-dom";
import { common_utils } from "../utils/utils"



export var accountService = {

    accountInfo: function (username, ok) {

        common_utils.request({
            url: 'account' + '/' + username,
            method: 'GET',
            data: null,
            success: ok
        });
    },
    register: function (formData, ok) {

        const data = new FormData();
    // 将 formData 中的字段添加到 FormData
    for (const key in formData) {
      if (formData[key] !== null && formData[key] !== undefined) {
        data.append(key, formData[key]);
      }
    }
        common_utils.formRequest({
            url: 'account',
            method: 'POST',
            data: data,
            success: ok
        });
    },
    login: function (username, password, ok) {
         console.log(username, password);
        common_utils.request({
            url: 'token',
            method: 'POST',
            data: { "username": username, "password": password },
            success: ok
        });
    },
    updateAccount: function (username, data, ok) {
        common_utils.request({
            url: 'account/' + username,
            method: 'PUT',
            data: data,
            success: ok
        });
    },
    logout: function () {
        // 退出登录
        localStorage.removeItem('token');
        localStorage.removeItem('user');
        window.location.href = '/login';
          
    },
    deleteAccount: function (username, ok) {
        common_utils.request({
            url: 'account/' + username,
            method: 'DELETE',
            data: null,
            success: ok
        });
    }

}






